package com.czwj.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Equipment;
import com.czwj.entities.Goods;
import com.czwj.entities.GoodsCurrent;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.GoodsCurrent;
import com.czwj.entities.Goods;
import com.czwj.entities.GoodsCurrent;
import com.czwj.entities.Goods;
import com.czwj.entities.UserInfo;
import com.czwj.service.GoodsCurrentService;
import com.czwj.service.GoodsService;
import com.czwj.util.TextUtil;

/**
* @ClassName: GoodsAction
* @Description: TODO
* @author H
* @date 2020年4月24日
*
*/
@Controller
public class GoodsAction{
	@Resource
	GoodsService goodsService;
	
	@Resource
	GoodsCurrentService goodsCurrentService;
	
	@RequestMapping(value = "/goodss",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Goods> goods(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "status",required = false) String status,
			@RequestParam(value = "name",required = false) String name,
			@RequestParam(value = "projectitemid",required = false) String projectitemid,
			@RequestParam(value = "projectitemchildid",required = false) String projectitemchildid){
		DataGridData<Goods> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status",status);
		}
		if(TextUtil.isNotEmpty(name)){
			map.put("name",name);
		}
		if(TextUtil.isNotEmpty(projectitemid)){
			map.put("projectitemid",projectitemid);
		}
		if(TextUtil.isNotEmpty(projectitemchildid)){
			map.put("projectitemchildid",projectitemchildid);
		}
		List<Goods> list = goodsService.search(map);
		result.setRows(list);
		result.setTotal(goodsService.count(map));
		return result;
	}
	
	@SystemControllerLog(description = "修改物资")
	@RequestMapping(value = "/goodsSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult goodsSave(@RequestParam(value = "images",required = false) MultipartFile[] images,
			@RequestParam(value = "id",required = false) Integer id,
			@RequestParam(value = "projectitemid",required = true) Integer projectitemid,
			@RequestParam(value = "projectitemchildid",required = true) Integer projectitemchildid,
			@RequestParam(value = "name",required = false) String name,
			@RequestParam(value = "specs",required = false) String specs,
			@RequestParam(value = "unit",required = false) String unit,
			@RequestParam(value = "initial",required = false) String initial,
			@RequestParam(value = "remark",required = false) String remark,
			@RequestParam(value = "actual",required = false) String actual,
			@RequestParam(value = "status",required = false) Integer status,HttpServletRequest request){
		OperateResult result = new OperateResult();
		int count = 0;
		Subject currentUser = SecurityUtils.getSubject();
		int createrid = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId();
		String createip = request.getRemoteAddr();
		count = goodsService.insertorupdate(id,projectitemid,projectitemchildid,name,specs,unit,initial,remark,actual,status,
				createrid,createip,images);
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "删除物资")
	@RequestMapping(value = "goodsDelete.do",method = RequestMethod.DELETE)
	@ResponseBody
	public OperateResult goodsDelete(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		int count = goodsService.delete(id);
		result.setSuccess(count > 0);
		return result;
	}
	
	@RequestMapping(value = "/goodscurrents",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<GoodsCurrent> goodscurrents(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "goodsid",required = false) String goodsid,
			@RequestParam(value = "startdate",required = false) String startdate,
			@RequestParam(value = "enddate",required = false) String enddate){
		DataGridData<GoodsCurrent> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate",startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate",enddate);
		}
		if(TextUtil.isNotEmpty(goodsid)){
			map.put("goodsid",goodsid);
		}
		List<GoodsCurrent> list = goodsCurrentService.search(map);
		result.setRows(list);
		result.setTotal(goodsCurrentService.count(map));
		return result;
	}
	
	@SystemControllerLog(description = "新增物资流水")
	@RequestMapping(value = "/goodsCurrentSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult goodsCurrentSave(@RequestBody GoodsCurrent goodsCurrent,HttpServletRequest request){
		OperateResult result = new OperateResult();
		Subject currentUser = SecurityUtils.getSubject();
		int createrid = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId();
		String createip = request.getRemoteAddr();
		goodsCurrent.setStatus(6);
		goodsCurrent.setCreatedate(new Date());
		goodsCurrent.setCreateip(createip);
		goodsCurrent.setCreaterid(createrid);
		int count = goodsCurrentService.insert(goodsCurrent);
		result.setSuccess(count > 0);
		return result;
	}
	
	@RequestMapping(value = "/reviewed/goods.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Goods> reviewedGoods(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "startdate",required = false) String startdate,
			@RequestParam(value = "enddate",required = false) String enddate,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<Goods> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate",startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate",enddate);
		}
		if(TextUtil.isNotEmpty(projectitemid) && !projectitemid.equals("0")){
			map.put("projectitemid",projectitemid);
		}
		map.put("status","4");
		result.setRows(goodsService.searchbylevel2(map));
		result.setTotal(goodsService.countbylevel2(map));
		return result;
	}
	
	@RequestMapping(value = "/review/goods.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Goods> unreviewedGoods(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<Goods> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(TextUtil.isNotEmpty(projectitemid) && !projectitemid.equals("0")){
			map.put("projectitemid",projectitemid);
		}
		map.put("status",6);
		result.setRows(goodsService.searchbylevel2(map));
		result.setTotal(goodsService.countbylevel2(map));
		return result;
	}
	
	@RequestMapping(value = "/review/goodscurrent.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<GoodsCurrent> unreviewedGoodsCurrent(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<GoodsCurrent> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(TextUtil.isNotEmpty(projectitemid) && !projectitemid.equals("0")){
			map.put("projectitemid",projectitemid);
		}
		map.put("status",6);
		result.setRows(goodsCurrentService.searchbylevel2(map));
		result.setTotal(goodsCurrentService.countbylevel2(map));
		return result;
	}
	
	@RequestMapping(value = "/reviewed/goodscurrent.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<GoodsCurrent> reviewedGoodsCurrent(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "startdate",required = false) String startdate,
			@RequestParam(value = "enddate",required = false) String enddate,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<GoodsCurrent> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate",startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate",enddate);
		}
		if(TextUtil.isNotEmpty(projectitemid) && !projectitemid.equals("0")){
			map.put("projectitemid",projectitemid);
		}
		map.put("status","4");
		result.setRows(goodsCurrentService.searchbylevel2(map));
		result.setTotal(goodsCurrentService.countbylevel2(map));
		return result;
	}
	
	@RequestMapping(value = "/GoodsReviewed.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Goods> GoodsReviewed(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows){
		DataGridData<Goods> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(userInfo.getRoleid() == 1 || userInfo.getRoleid() == 2){
		}else if(userInfo.getRoleid() == 3){
			map.put("status","3,4");
			result.setRows(goodsService.searchReviewedLevel2(map));
			result.setTotal(goodsService.countReviewedLevel2(map));
		}
		return result;
	}
	
	@RequestMapping(value = "/GoodsCurrentReviewed.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<GoodsCurrent> GoodsCurrentReviewed(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows){
		DataGridData<GoodsCurrent> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(userInfo.getRoleid() == 1 || userInfo.getRoleid() == 2){
		}else if(userInfo.getRoleid() == 3){
			map.put("status","3,4");
			result.setRows(goodsCurrentService.searchReviewedLevel2(map));
			result.setTotal(goodsCurrentService.countReviewedLevel2(map));
		}
		return result;
	}
}
